//index.js
const app = getApp()
const { envList } = require('../../envList.js')

Page({
  data: {
    stage: ['总榜', '月榜', '周榜', '日榜'],
    i: 0,
    person: []
  },
  onLoad: function (options) {
    // Do some initialize when page load.

    this.getPerson()
  },
  onShow: function () {
    // Do something when page show.
  },
  upScore: function (e) {
    if (wx.getStorageSync('isAdmin') == true) {
      wx.showModal({
        title: '提示',
        content: '要给' + e.currentTarget.dataset.name + '增加积分吗？',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.showLoading({
              title: '加载中',
            })
            wx.cloud.callFunction({
              name: 'classCounter',
              data: {
                type: 'upScore',
                ids: [e.currentTarget.dataset.id]
              }
            }).then(res => {
              if (res.result.success) {
                wx.showToast({
                  title: res.result.data,
                  icon: 'success',
                  duration: 2000,
                })
              } else {
                wx.showToast({
                  title: res.result.data,
                  icon: 'error',
                  duration: 2000
                })
              }
              setTimeout(function () {
                wx.hideLoading()
              }, 2000)
            }).catch(console.error)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      i: e.detail.value
    }, () => {
      this.getPerson()
    })
  },
  getPerson() {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'classCounter',
      data: {
        type: 'list',
        stage: this.data.stage[this.data.i]
      }
    }).then(res => {
      console.log(res)
      this.setData({
        person: res.result.data
      })
      wx.hideLoading()
    }).catch(console.error)
  }
})
